package com.ruoyi.workflow.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.workflow.domain.WfFormData;
import com.ruoyi.workflow.domain.bo.WfFormDataBo;
import com.ruoyi.workflow.domain.vo.WfFormDataVo;
import com.ruoyi.workflow.mapper.WfFormDataMapper;
import com.ruoyi.workflow.service.IWfFormDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 流程单数据Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-18
 */
@RequiredArgsConstructor
@Service
public class WfFormDataServiceImpl implements IWfFormDataService {

    private final WfFormDataMapper baseMapper;

    /**
     * 查询流程单数据
     */
    @Override
    public WfFormDataVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询流程单数据列表
     */
    @Override
    public TableDataInfo<WfFormDataVo> queryPageList(WfFormDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WfFormData> lqw = buildQueryWrapper(bo);
        Page<WfFormDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询流程单数据列表
     */
    @Override
    public List<WfFormDataVo> queryList(WfFormDataBo bo) {
        LambdaQueryWrapper<WfFormData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WfFormData> buildQueryWrapper(WfFormDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WfFormData> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getProcessdefid()), WfFormData::getProcessdefid, bo.getProcessdefid());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), WfFormData::getContent, bo.getContent());
        return lqw;
    }

    /**
     * 新增流程单数据
     */
    @Override
    public Boolean insertByBo(WfFormDataBo bo) {
        WfFormData add = BeanUtil.toBean(bo, WfFormData.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改流程单数据
     */
    @Override
    public Boolean updateByBo(WfFormDataBo bo) {
        WfFormData update = BeanUtil.toBean(bo, WfFormData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WfFormData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除流程单数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public WfFormDataVo queryByprocessdefid(String processdefid) {
        //Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WfFormData> lqw = Wrappers.lambdaQuery();
        lqw.eq(WfFormData::getProcessdefid, processdefid);
        return baseMapper.selectVoOne(lqw);
    }
}
